package scau.online.web.admin;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import scau.cty.base.common.BaseController;
import scau.cty.base.common.IBaseService;
import scau.cty.dictionary.Code;
import scau.cty.entity.User;
import scau.cty.service.IUserService;

import java.math.BigDecimal;
import java.util.*;

/**
 * Created by caoxiong on 2017/4/17.
 * 管理员 用户账号管理器
 */
@Api(value = "用户账号信息管理控制器",description = "用户账号信息接口")
@RestController
@RequestMapping(value = "/admin_user_account",produces = "application/json")
public class AdminUsersAccountController extends BaseController{
    @Autowired
    private IBaseService<User> userIBaseService;
    /**
     *用户账户列表  根据pageSize和pageNow查询 order为排序方式 空或者为1为按照id 排列 其余请情况按照username 排列
     * @param pageSize  每页的数量
     * @param pageNow   当前页
     * @param order     排列方式
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "用户账号列表接口")
    @ApiImplicitParams({@ApiImplicitParam(name = "pageSize",value = "每页数量",required = true,paramType = "query"),
            @ApiImplicitParam(name = "pageNow",value = "请求页码",required = true,paramType = "query"),
            @ApiImplicitParam(name = "order",value = "排序方式，不传参数或参数为0为默认排序，其他情况按照username排序",
                    required = true,paramType = "query")
    })
    @GetMapping(value = "/user_list")
    public String getUserList(Integer pageSize,Integer pageNow,Integer order) throws Exception{
        Map map = new LinkedHashMap<>();
        if(pageNow == null || pageNow == 0){
            pageNow = 1;
        }
        if(pageSize == null || pageSize ==0){
            sendCode(map, Code.ILLEGAL_DATA_ERROR);
            return JSON.toJSONString(map);
        }
        if(order == null || order == 0 ){
            //默认排序
            List<User> userList = userIBaseService.selectPageEntityOrderByProperty(pageNow,pageSize,"id");
            map.put("userList",userList);
        }else {
            List<User> userList = userIBaseService.selectPageEntityOrderByProperty(pageNow,pageSize,"username");
            map.put("userList",userList);
        }
        sendCode(map,Code.OPERATION_SUCCESS);
        return JSON.toJSONString(map);
    }

    /**
     * 修改用户信息 通过用户唯一id 标识 修改 除了id 和 创建时间不可修改，其他的可以修改
     *  id
     * username
     *  password
     *  salt
     *  role_rank
     * @return
     */
    @ApiOperation(value = "修改用户账号信息接口")
    @ApiImplicitParams({@ApiImplicitParam(name = "id",value = "商品唯一标识id",required = true,paramType = "query"),
            @ApiImplicitParam(name = "username",value = "用户名",required = true,paramType = "query"),
            @ApiImplicitParam(name = "password",value = "用户账号密码",required = true,paramType = "query"),
            @ApiImplicitParam(name = "salt",value = "用户账号加密盐",required = true,paramType = "query"),
            @ApiImplicitParam(name = "roleRank",value = "用户账号等级",required = true,paramType = "query")})
    @PostMapping(value = "/user_update")
    public String updateUserInformationById(User user) {
        Map map = new LinkedHashMap();
        if (user.getId() == null || user.getId() == "") {
            sendCode(map, Code.ILLEGAL_DATA_ERROR);
            return JSON.toJSONString(map);
        }
        if(user.getUsername() == null && user.getPassword() == null && user.getSalt() == null){
            sendCode(map, Code.ILLEGAL_DATA_ERROR);
            return JSON.toJSONString(map);
        }
        int updateUser = userIBaseService.updateNotNull(user);
        map.put("updateUser", updateUser);
        sendCode(map, Code.OPERATION_SUCCESS);
        return JSON.toJSONString(map);
    }

    /**
     * 通过用户id 查询该用户的所有信息
     * @param id
     * @return
     */
    @ApiOperation(value = "用户账号详情接口")
    @ApiImplicitParams({@ApiImplicitParam(name = "id",value = "用户账号唯一标识id",required = true,paramType = "query")})
    @GetMapping(value = "/user_select")
    public String selectUserById(String id){
        Map map = new LinkedHashMap();
        if(id == null || id ==""){
            sendCode(map,Code.ILLEGAL_DATA_ERROR);
            return JSON.toJSONString(map);
        }
        User userColumnList = userIBaseService.findByKey(id);
        map.put("userColumn", userColumnList);
        sendCode(map,Code.OPERATION_SUCCESS);
        return JSON.toJSONString(map);
    }

    /**
     * 通过用户的 id 删除该用户
     * @param id
     * @return
     */
    @ApiOperation(value = "删除用户账号接口")
    @ApiImplicitParams({@ApiImplicitParam(name = "id",value = "用户账号唯一标识id",required = true,paramType = "query")})
    @PostMapping(value = "/user_delete")
    public String deleteUserById(String id){
        Map map = new LinkedHashMap();
        if(id == null || id == ""){
            sendCode(map,Code.ILLEGAL_DATA_ERROR);
            return JSON.toJSONString(map);
        }
        int deleteUser = userIBaseService.delete(id);
        map.put("deleteUser",deleteUser);
        sendCode(map,Code.OPERATION_SUCCESS);
        return  JSON.toJSONString(map);
    }

    /**
     * 增加用户
     * @param user
     * @return
     */
    @ApiOperation(value = "增加用户账号接口")
    @ApiImplicitParams({@ApiImplicitParam(name = "username",value = "用户名",required = true,paramType = "query"),
            @ApiImplicitParam(name = "password",value = "用户账号密码",required = true,paramType = "query"),
            @ApiImplicitParam(name = "salt",value = "用户账号加密盐",required = true,paramType = "query"),
            @ApiImplicitParam(name = "roleRank",value = "用户账号等级",required = true,paramType = "query")})
    @PostMapping(value = "/user_add")
    public String addUsers(User user){
        Map map = new LinkedHashMap();
        if(user.getRoleRank() == null || user.getRoleRank() == 0){
            user.setRoleRank(1);
        }
        if(user.getUsername() == null || user.getUsername() == "" || user.getPassword() == null || user.getPassword() == ""
                || user.getSalt() == null || user.getSalt() == "" ){
            sendCode(map,Code.ILLEGAL_DATA_ERROR);
            return JSON.toJSONString(map);
        }
        user.setCreateTime(new Date());
        int addUser = userIBaseService.add(user);
        map.put("addUser",addUser);
        sendCode(map,Code.OPERATION_SUCCESS);
        return JSON.toJSONString(map);
    }
}
